using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ninject;
using Tetris.Interfaces;
using Tetris.Utility;

namespace Tetris.Screens
{
    internal class NotificationScreen : GameScreen
    {
        private readonly string _text;
        private Vector2 _textSize;
        private SpriteFont _font;
        private int _startSecond;
        private int _secondsVisible;

        public NotificationScreen(string text, int secondsVisible)
        {
            _text = text;
            _secondsVisible = secondsVisible;
        }

        public override void Initialize(IGameManager gameManager)
        {
            _startSecond = gameManager.GameTime.TotalGameTime.Seconds;
            TransitioningOutTime = TimeSpan.FromSeconds(_secondsVisible);
            IsPopup = true;
            _font = gameManager.ServiceLocator.Get<IContentFactory>().MenuFont;
            _textSize = _font.MeasureString(_text);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            int nowSeconds = gameTime.TotalGameTime.Seconds;
            if (nowSeconds - _startSecond == _secondsVisible)
            {
                ScreenManager.RemoveScreen(this);
            }
        }


        public override void Draw(SpriteBatch spriteBatch, IGameManager gameManager, GameTime gameTime)
        {
            Viewport viewport = gameManager.Game.GraphicsDevice.Viewport;
            spriteBatch.Begin();
            spriteBatch.Draw(GraphicUtils.CreateRectangle(gameManager.Game, (int)(_textSize.X + 40), (int)(_textSize.Y), Color.Black * 0.1f), new Vector2(viewport.Bounds.Center.X - (_textSize.X+40) / 2, viewport.Bounds.Bottom - 50), Color.White * 0.5f);
            spriteBatch.DrawString(_font, _text, new Vector2(viewport.Bounds.Center.X - _textSize.X/2,viewport.Bounds.Bottom - 50),Color.PapayaWhip );
            spriteBatch.End();
        }
    }
}